﻿using System;
using UnityEditor;
using UnityEngine;
using CinemaDirector;
using TTGameEngine;

[CustomEditor(typeof(SkillPlayAnimEvent))]
public class SkillPlayAnimEventEditor : Editor {
    // Properties
    private SerializedObject skillPlayAnimEventObj;
    SkillPlayAnimEvent skillPlayAnimEvent;
    GUIContent trimTimeContent = new GUIContent("Trim Time", "Trim Time For AnimInfo.AnimTime");

    /// <summary>
    /// On inspector enable, load serialized objects
    /// </summary>
    public void OnEnable() {
        skillPlayAnimEvent = target as SkillPlayAnimEvent;
        skillPlayAnimEventObj = new SerializedObject(this.target);
    }

    /// <summary>
    /// Update and Draw the inspector
    /// </summary>
    public override void OnInspectorGUI() {
        GUI.enabled = false;
        //只读，不可修改
        EditorGUILayout.TextField("Event End Time: ", (skillPlayAnimEvent.EndTime).ToString());
        GUI.enabled = true;

        base.OnInspectorGUI();

        skillPlayAnimEventObj.Update();

        AnimationState clip = null;
        string animName = "";
        string objName = "";
        string strHint = "";
        Animation anim = getTargetAnim(out objName);
        if(anim == null) {
            strHint = string.Format("提示：没有设置测试Actor，找不到相应Puppet的Animation组件，无法进行错误检测...");
        } else {
            animName = skillPlayAnimEvent.animName;
            clip = anim[animName];
            if(clip == null) {
                strHint = string.Format("错误：动画名[{1}]不对不对不对。 Obj:[{0}] ", objName, animName);
            }
        }
        GUI.enabled = false;
        EditorGUILayout.ObjectField("Target Anim: ", anim, typeof(Animation), true);
        GUI.enabled = true;
        //trim anim time
        if(GUILayout.Button(trimTimeContent)) {
            if(clip != null) {
                skillPlayAnimEvent.animTime = clip.length;
                skillPlayAnimEvent.Duration = clip.length;
            }
        }

        if(false == string.IsNullOrEmpty(strHint)) {
            GUILayout.BeginVertical("HelpBox");
            EditorGUILayout.LabelField(strHint);
            GUILayout.EndVertical();
        }

        skillPlayAnimEvent.animTime = skillPlayAnimEvent.Duration;

        skillPlayAnimEventObj.ApplyModifiedProperties();

    }

    Animation getTargetAnim(out string objName) {
        objName = "";
        if(skillPlayAnimEvent.TimelineTrack == null) {
            return null;
        }
        ActorTrackGroup atg = skillPlayAnimEvent.TimelineTrack.TrackGroup as ActorTrackGroup;
        if(atg == null) {
            return null;
        }
        Animation anim = null;
        Transform atgDebugTF = (atg.Actor != null ? atg.Actor : atg.DebugActor);
        if(atgDebugTF == null) {
            anim = atgDebugTF.GetComponentInChildren<Animation>();
        }

        //找Puppet
        if(anim == null) {
            Puppet[] puppets = skillPlayAnimEvent.GetComponentsInParent<Puppet>(true);
            if(puppets.Length > 0) {
                Puppet puppet = puppets[0];
                objName = puppet.name;
                anim = puppet.Anim;
                if(anim != null) {
                    anim = puppet.GetComponentInChildren<Animation>();
                }
            }
        }

        return anim;

    }
}



